<template>
  <el-container class="home-container">
    <el-header>
      <div><span>图书管理系统</span></div>
      <el-button type="info" @click="logout">exit</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu background-color="rgb(51 55 68)" text-color="#fff" active-text-color="#409eff" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="item.avator"></i>
              <span>{{item.authname}}</span>
            </template>
            <el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/' + subitem.path)">
              <i class="el-icon-menu"></i>
              <span>{{subitem.authname}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getInfo } from '@/utils/http'
export default {
  data () {
    return {
      menuList: [],
      // 菜单折叠
      isCollapse: false,
      // 激活左侧（高亮nav）
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      this.$messageBox.confirm('确定退出吗？').then(() => {
        window.sessionStorage.clear()
        this.$router.push('/login')
        this.$message.success('成功退出')
      }).catch(err => console.log(err))
    },
    // 获取所有菜单
    async getMenuList () {
      // const { data: res } = await this.$http.get('menus')
      const { data: res } = await getInfo('menus')
      // console.log(res)
      if (res.status !== '200') return this.$message.error('查询列表失败！')
      this.menuList = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存状态栏激活
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373D41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #EAEDF1;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: white;
  text-align: center;
  letter-spacing: .2em;
  cursor: pointer;
}
</style>
